package lib.core.view
{
	import flash.events.Event;
	
	import lib.core.com.BaseApplication;
	import lib.core.com.modules.BaseModule;
	import lib.core.controller.RequestUrlInfo;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import urlkit.rules.FlexBrowserManagerAdapter;
	
	public class BaseModuleMediator extends Mediator implements IMediator
	{
		private var _request:RequestUrlInfo;
		
		protected function get request():RequestUrlInfo
		{
			return this._request;
		}
		
		protected function get module():BaseModule
		{
			return this.viewComponent as BaseModule;
		}
		
		public function BaseModuleMediator(mediatorName:String, viewComponent:BaseModule)
		{
			super(mediatorName, viewComponent);
			this.module.addEventListener(Event.REMOVED, onModuleRemoved);
		}
		
		public function initModule(request:RequestUrlInfo):void
		{
			this._request = request;
			this.onInitModule();
		}
		
		protected function onInitModule():void
		{
		}
		
		public function preExecute():void
		{	
		}
		
		override public function handleNotification(notification:INotification):void
		{
			// TODO: Check if this fix is actually needed
			/*
			if (this.module.parent == null)
			{
				(this.module.parentApplication as BaseApplication).container.removeChildAt(0);
				(this.module.parentApplication as BaseApplication).container.addChildAt(this.module, 0);
			}*/
			this.onHandleNotification(notification);
		}
				
		protected function onHandleNotification(notification:INotification):void
		{
			
		}
		
		private function onModuleRemoved(e:Event):void
		{
			this.onExit();
		}
		
		protected function onExit():void
		{
			
		}
		
		protected function goTo(url:String):void
		{
			FlexBrowserManagerAdapter.getInstance().navigate(url);
		}
	}
}